using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OurMediaPlayer.View;

namespace OurMediaPlayer.View
{
    public partial class SliderForm : Form
    {
        private System.Windows.Forms.Timer guiTimer;

		public enum ESlideDirection {Top, Left, Bottom, Right};
		
        protected ESlideDirection slideDirection_;

        protected Form oOwner;
        private float fRatio; 
		private float fStep;
		private bool bExpand;
		private SizeF oOffset;
		private SizeF oStep;
		private Point oOrigin;
		/// <summary>
		/// Return the state of the form (expanded or not)
		/// </summary>
		public bool IsExpanded
		{
			get
			{
				return bExpand;
			}
		}
		/// <summary>
		/// Direction of sliding
		/// </summary>
		public ESlideDirection SlideDirection
		{
			set
			{
				slideDirection_ = value;
			}
		}
		/// <summary>
		/// Slide step of the motion
		/// </summary>
		public float SlideStep
		{
			set
			{
				fStep = value;
			}
		}
		/// <summary>
		/// Default constructor
		/// </summary>
		public SliderForm() : this(null, 0)
		{
		}
		/// <summary>
		/// Constructor with parent window and step of sliding motion
		/// </summary>
		public SliderForm(Form poOwner, float pfStep)
		{
			InitializeComponent();
            oOwner = poOwner;
			fRatio = 0.0f;
			SlideStep = pfStep;
			if (this.Owner != null)
				Owner = poOwner.Owner;
		}
		
		public void Slide()
		{
			if (!bExpand)
				Show();
			oOwner.BringToFront();
			bExpand = !bExpand;
			guiTimer.Start();
		}

        private void guiTimer_Tick(object sender, System.EventArgs e)
		{
			if (bExpand)
			{
				fRatio += fStep;
				oOffset += oStep;
				if (fRatio >= 1)
					guiTimer.Stop();
			}
			else
			{
				fRatio -= fStep;
				oOffset -= oStep;
				if (fRatio <= 0)
					guiTimer.Stop();
			}
			SetLocation();
		}
		private void SetLocation()
		{
			Location = oOrigin + oOffset.ToSize();
		}

		private void SlideDialog_Move(object sender, System.EventArgs e)
		{
			SetSlideLocation();
			SetLocation();
		}

		private void SlideDialog_Resize(object sender, System.EventArgs e)
		{
			SetSlideLocation();
			SetLocation();
		}
		private void SlideDialog_Closed(object sender, System.EventArgs e)
		{
			Close();
		}

		private void SetSlideLocation()
		{
			if (oOwner != null)
			{
				oOrigin = new Point();
				switch (slideDirection_)
				{
					case ESlideDirection.Bottom:
						oOrigin.X = oOwner.Location.X;
						oOrigin.Y = oOwner.Location.Y + oOwner.Height - Height;
						Width = oOwner.Width;
						oStep = new SizeF(0, Height * fStep);
						break;
					case ESlideDirection.Left:
						oOrigin = oOwner.Location;
						oStep = new SizeF(- Width * fStep, 0);
						Height = oOwner.Height;
						break;
					case ESlideDirection.Top:
						oOrigin = oOwner.Location;
						Width = oOwner.Width;
						oStep = new SizeF(0, - Height * fStep);
						break;
					case ESlideDirection.Right:
						oOrigin.X = oOwner.Location.X + oOwner.Width - Width;
						oOrigin.Y = oOwner.Location.Y;
						oStep = new SizeF(Width * fStep, 0);
						Height = oOwner.Height;
						break;
				}
			}
		}
		protected override void OnLoad(System.EventArgs e)
		{
			SetSlideLocation();
			SetLocation();
			if (oOwner != null)
			{
				oOwner.LocationChanged += new System.EventHandler(this.SlideDialog_Move);
				oOwner.Resize += new System.EventHandler(this.SlideDialog_Resize);
				oOwner.Closed += new System.EventHandler(this.SlideDialog_Closed);
			}
		}
    }
}